<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('移动策略')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="400px" @close='close'>
    <el-form ref="form" :model="record" label-width="120px" :rules="rules">
      <el-form-item :label="$t('需要移动的策略')" prop="name">
        <el-input v-emoji v-model="record.name" ref="name" style="width:217px;" disabled size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('移动至策略')" prop="name_target">
        <el-select v-model="record.name_target" ref="nameTarget" :placeholder="$t('请选择')" size='small' filterable clearable>
          <el-option v-for="item in nameTargetList" :key="item.name" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('前/后')" prop="dir">
        <el-select v-model="record.dir" ref="dir" :placeholder="$t('请选择')" size='small'>
          <el-option v-for="item in dirList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getArray, moveInfo } from '@/api/network/route/policyRoute'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'moveDialog',
  data () {
    var validateDistId = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请选择移动至的策略')))
      } else {
        callback()
      }
    }
    var validatedir = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请选择目的位置')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        name_target: '',
        dir: 'before'
      },
      recordCopy: {
        name: '',
        name_target: '',
        dir: 'before'
      },
      dirList: [{ label: this.$t('前'), value: 'before' }, { label: this.$t('后'), value: 'after' }],
      nameTargetList: [],
      rules: {
        name_target: [{ required: false, validator: validateDistId, trigger: 'blur' }],
        dir: [{ required: false, validator: validatedir, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.record.name = data.name
      this.$nextTick(() => {
        const data = ['name']
        const selectData = ['nameTarget', 'dir']
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      getArray().then(res => {
        if (res.status === 0) {
          this.nameTargetList = res.info
          for (let item in this.nameTargetList) {
            if (data.name === this.nameTargetList[item].name) {
              this.nameTargetList.splice(item, 1)
              break
            }
          }
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          moveInfo(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('移动成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.nameTargetList = []
      this.$refs.form.resetFields()
    }
  }
}
</script>
